package com.yeren.json.tarena.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.yeren.json.tarena.bean.City;

public class ActionServlet extends HttpServlet{
	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String uri=request.getRequestURI();
		String action=uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
		if(action.equals("/city")){
			String name=request.getParameter("name");
			if(name.equals("bj")){
				City c1=new City("海淀","hd");
				City c2=new City("东城","dc");
				City c3=new City("西城","xc");
				List<City> cs=new ArrayList<City>();
				cs.add(c1);
				cs.add(c2);
				cs.add(c3);
				JSONArray obj=JSONArray.fromObject(cs);
				String str=obj.toString();
				out.print(str);
			}else if (name.equals("sh")) {
				City c1=new City("徐汇","xh");
				City c2=new City("静安","ja");
				City c3=new City("黄埔","hp");
				List<City> cs=new ArrayList<City>();
				cs.add(c1);
				cs.add(c2);
				cs.add(c3);
				JSONArray obj=JSONArray.fromObject(cs);
				String str=obj.toString();
				out.print(str);
			}
		}
		out.close();
	}
}
